package org.abratuhi.kvngroupware.scenario.mptua;

import org.abratuhi.kvngroupware.network.AbstractRequestMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.scenario.manager.ScenarioManager;
import org.abratuhi.kvngroupware.server.db.ServerDB;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.jdom.Element;

public class ScenarioJokeRequestMessageProceeder extends AbstractRequestMessageProceeder{

	MessageSender sender;
	UserManager usermanager;
	ScenarioManager scenariomanager;
	ServerDB db;

	public ScenarioJokeRequestMessageProceeder(MessageSender sender, UserManager usermanager, ScenarioManager scenariomanager, ServerDB db){
		this.sender = sender;
		this.usermanager = usermanager;
		this.scenariomanager = scenariomanager;
		this.db = db;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element request = message.getRoot().getChild("request");
		String type = request.getAttributeValue("type");
		String action = request.getAttributeValue("action");
		String jokeId = request.getAttributeValue("jokeId");
		String scenarioId = request.getAttributeValue("scenarioId");

		String[] attrNames = new String[]{"type", "action", "jokeId", "scenarioId", "status"};
		String[] attrValues= new String[]{type, action, jokeId, scenarioId, "ok"};
		String text = new String();
		Element[] content = new Element[0];
		Message responseMessage;

		if(type.equals("scenario_joke")){
			if(action.equals("add")){
				//content = (Element[]) request.removeContent().toArray(new Element[request.getContentSize()]);
				//for(int i=0; i<content.length; i++) kvnserver.getDB().addScenarioJoke(scenarioId, content[i]);
				db.addScenarioJoke(scenarioId, jokeId);
			}
			else if(action.equals("remove")){
				db.removeScenarioJoke(scenarioId, jokeId);
			}
			else if(action.equals("up")){
				db.upScenarioJoke(scenarioId, jokeId);
			}
			else if(action.equals("down")){
				db.downScenarioJoke(scenarioId, jokeId);
			}

			responseMessage = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(responseMessage);
		}
	}

}
